package com.juvepoland.jpdroid.activities;

import java.text.Format;
import java.text.SimpleDateFormat;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.application.JPdroidApplication;
import com.juvepoland.jpdroid.common.ImageLoader;
import com.juvepoland.jpdroid.managers.impl.CommentManager;
import com.juvepoland.jpdroid.managers.impl.NewsManagerImpl;
import com.juvepoland.jpdroid.models.Comment;
import com.juvepoland.jpdroid.models.News;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SingleCommentActivity extends BasicActivity {
	
	private int newsId;
	private int commentId;
	
	private News news;
	private Comment comment;
	
	NewsManagerImpl newssManager = new NewsManagerImpl();
	CommentManager commentManager = new CommentManager();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.single_news_activity_main_layout_comment);
		Intent intent = getIntent();
		newsId = intent.getIntExtra("news_id", 0);
		commentId = intent.getIntExtra("comment_id", 0);
			
	new AsyncActionClass().execute(null);
		
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		JPdroidApplication app = (JPdroidApplication)getApplicationContext();
		MenuItem replyMenuItem = menu.findItem(R.id.reply_bookmark);
		if(app.isUserLogged()){
			if(replyMenuItem!=null){
				replyMenuItem.setVisible(true);
			}
		}
		else{
			if(replyMenuItem!=null){
				replyMenuItem.setVisible(false);
			}
		}
		return super.onPrepareOptionsMenu(menu);
	}
	
		
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.reply_bookmark)
		{
				Intent replyIntent = new Intent(getApplicationContext(), SendCommentActivity.class);
				replyIntent.putExtra("reply_username", comment.getUser().getUsername());
				replyIntent.putExtra("news_id", news.getId());
				startActivity(replyIntent);
				return true;
		}
		else if(item.getItemId() == R.id.comment_bookmark){
			if(news.getCommentEnabled()==1){
				Intent sendCommentIntent = new Intent(getApplicationContext(), SendCommentActivity.class);
				sendCommentIntent.putExtra("news_id", news.getId());
				startActivity(sendCommentIntent);
			}
			else{
				Toast.makeText(getApplicationContext(), "Dodawanie komentarzy do tego newsa jest zablokowane.",
						Toast.LENGTH_LONG).show();
			}
			return true;
		}
		else{
			return super.onOptionsItemSelected(item);
		}
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.layout.single_comment_menu, menu);
		return true;
	}

	@Override
	protected void doInBackgroundAsyncProcess() throws Exception {
		if(newsId >0){
			news = newssManager.getNews(newsId);
			TextView tv = (TextView) findViewById(R.id.news_content);
			news.setContent(news.getContent().replaceAll("(\r\n|\n)", "<br />"));
			tv.setText(Html.fromHtml(news.getContent()));
			tv.setSingleLine(false);
			tv.setTypeface(basicFontTypeFace);
			
			tv = (TextView) findViewById(R.id.title_single);
			tv.setText(news.getTitle());
			tv.setTypeface(basicFontTypeFace);
			
			tv = (TextView) findViewById(R.id.date_single);
			tv.setText(news.getStringDate());
			tv.setTypeface(basicFontTypeFace);
			
			tv = (TextView) findViewById(R.id.source_news);
			tv.setTypeface(basicFontTypeFace);
			tv.setText(news.getSource());
			
			ImageLoader il = new ImageLoader(getApplicationContext());
			ImageView thumb_image=(ImageView)findViewById(R.id.single_image); // thumb image
			il.DisplayImage("http://www.juvepoland.com/images/news/"+news.getId()+".jpg", thumb_image);
		}
		else{
			throw new Exception("Nie znaleziono wiadomości.");
		}
		if(commentId>0){
			comment = commentManager.getComment(commentId);
			
			 TextView contentTextView = (TextView)findViewById(R.id.comment_content);
		        TextView infoTextView = (TextView)findViewById(R.id.comment_info);
		        ImageView thumb_image=(ImageView)findViewById(R.id.comment_image);
		        TextView id = (TextView) findViewById(R.id.cid);
		        
		        ImageLoader il = new ImageLoader(getApplicationContext());
		        
		        comment.setContent(comment.getContent().replaceAll("(\r\n|\n)", "<br />"));
		        contentTextView.setText(Html.fromHtml(comment.getContent()));
		        contentTextView.setSingleLine(false);
		        
		        contentTextView.setTypeface(basicFontTypeFace);
		        infoTextView.setTypeface(basicFontTypeFace);
		        
		        id.setText(String.valueOf(comment.getId()));
		        infoTextView.setText(comment.getUser().getUsername()+" || "+comment.getStringDate());
		        infoTextView.setTypeface(basicFontTypeFace);
		        if(comment.getUser().getAvatarSource().length()>0){
		        	il.DisplayImage("http://forum.juvepoland.com/images/avatars/"+comment.getUser().getAvatarSource(), thumb_image);
		        }
		        
		       
		}
		else{
			throw new Exception("Nie znaleziono komentarza.");
		}
		
		
	}

	@Override
	protected Intent getIntentToReturnAfterLogout() {
		Intent intent = new Intent(getApplicationContext(), SingleNewsActivity.class);
		intent.putExtra("news_id", news.getId());
		return intent;
		
	}

}
